package com.xboard.server.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.xboard.server.dao.MovieDao;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/movie")
public class MovieController {

	@Autowired
	private MovieDao movieDao;

	@RequestMapping(value="/add", method = RequestMethod.GET)
	public ModelAndView add(HttpServletRequest request, ModelMap model) {
		return new ModelAndView("add");
	}	
	
	@RequestMapping(value="/add/{MOVIE_ID}/{RANK_NO}/{MOVIE_NAME}/{OPEN_DATE}/{TITLE_IMAGE}/{MOVIE_LINK}/{GRADE_SCORE}/{SHOW_COUNT}", 
			        method = RequestMethod.GET)
	public ModelAndView add(@PathVariable String MOVIE_ID, @PathVariable String RANK_NO, @PathVariable String MOVIE_NAME, @PathVariable String OPEN_DATE, @PathVariable String TITLE_IMAGE, @PathVariable String MOVIE_LINK, @PathVariable String GRADE_SCORE, @PathVariable String SHOW_COUNT,  
			HttpServletRequest request, ModelMap model) {
        
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("MOVIE_ID",MOVIE_ID);
		data.put("RANK_NO",RANK_NO);
		data.put("MOVIE_NAME",MOVIE_NAME);
		data.put("OPEN_DATE",OPEN_DATE);
		data.put("TITLE_IMAGE",TITLE_IMAGE);
		data.put("MOVIE_LINK",MOVIE_LINK);
		data.put("GRADE_SCORE",GRADE_SCORE);
		data.put("SHOW_COUNT",SHOW_COUNT);
		movieDao.addMovie(data);

		return new ModelAndView("redirect:/movie/list");
	}

	@RequestMapping(value="/list")
	public ModelAndView listMovie() {

		List<Map<String,Object>> list= new ArrayList<Map<String,Object>>();
		list = movieDao.listMovie();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("success",true);
		mav.addObject("rows",  list);
	    return mav;
	}
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/select/{MOVIE_ID}", method = RequestMethod.GET)
	public ModelAndView getMovie(@PathVariable String MOVIE_ID,
			HttpServletRequest request, ModelMap model) {

		Map<String, Object> result = movieDao.getMovie(MOVIE_ID);
		
		ModelAndView mav = new ModelAndView("update");
		mav.addObject("success",true);
		mav.addObject("row",  result);
		
		return mav;
	}
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/update/{MOVIE_ID}/{RANK_NO}/{MOVIE_NAME}/{OPEN_DATE}/{TITLE_IMAGE}/{MOVIE_LINK}/{GRADE_SCORE}/{SHOW_COUNT}", 
	                method = RequestMethod.GET)
	public ModelAndView updateMovie(@PathVariable String MOVIE_ID, @PathVariable String RANK_NO, @PathVariable String MOVIE_NAME, @PathVariable String OPEN_DATE, @PathVariable String TITLE_IMAGE, @PathVariable String MOVIE_LINK, @PathVariable String GRADE_SCORE, @PathVariable String SHOW_COUNT,  
			HttpServletRequest request, ModelMap model) {

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("MOVIE_ID",MOVIE_ID);
		data.put("RANK_NO",RANK_NO);
		data.put("MOVIE_NAME",MOVIE_NAME);
		data.put("OPEN_DATE",OPEN_DATE);
		data.put("TITLE_IMAGE",TITLE_IMAGE);
		data.put("MOVIE_LINK",MOVIE_LINK);
		data.put("GRADE_SCORE",GRADE_SCORE);
		data.put("SHOW_COUNT",SHOW_COUNT);
		movieDao.updateMovie(data);
		
		return new ModelAndView("redirect:/movie/list");

	}

	@SuppressWarnings("deprecation")
	@RequestMapping(value="/delete/{MOVIE_ID}", method = RequestMethod.GET)
	public ModelAndView delete(@PathVariable String MOVIE_ID,
			HttpServletRequest request, ModelMap model) {
		
		movieDao.deleteMovie(MOVIE_ID);
		return new ModelAndView("redirect:/movie/list");
	}

}
